//
//  IngredientDetailView.m
//  KeepFit
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "IngredientDetailView.h"
#define widthScreen [UIScreen mainScreen].bounds.size.width
#define heightScreen [UIScreen mainScreen].bounds.size.height
@implementation IngredientDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.upImageView = [[UIImageView alloc] init];
        [self addSubview:self.upImageView];
        self.upImageView.userInteractionEnabled = YES;
        self.returnButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.upImageView addSubview:self.returnButton];
        UIImage *image = [UIImage imageNamed:@"iconfont-fanhui@2x.png"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.returnButton setImage:image forState:UIControlStateNormal];
       
        self.titleLab = [[UILabel alloc] init];
        self.titleLab.textColor = [UIColor redColor];
        self.titleLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLab];
        
        self.descriptionLab = [[UILabel alloc] init];
        [self addSubview:self.descriptionLab];
        self.descriptionLab.numberOfLines = 0;
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.button setTitle:@"查看详情" forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor grayColor];
        self.tintColor = [UIColor whiteColor];
        [self addSubview:self.button];
        
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.upImageView.frame = CGRectMake(0, 0, self.frame.size.width, 200);
    self.returnButton.frame = CGRectMake(10, 10, 40, 40);
    self.titleLab.frame = CGRectMake( 0, self.upImageView.frame.size.height + 10, self.upImageView.bounds.size.width, 30);
    
    CGRect rect = [self.descriptionLab.text boundingRectWithSize:CGSizeMake(widthScreen - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    

    self.descriptionLab.frame = CGRectMake(20, self.upImageView.bounds.size.height + self.titleLab.bounds.size.height + 20, widthScreen - 40, rect.size.height);
    [self.descriptionLab sizeToFit];
  
    self.button.frame = CGRectMake(widthScreen / 2 - 60, self.upImageView.bounds.size.height+ self.titleLab.bounds.size.height + self.descriptionLab.bounds.size.height + 40, 120, 30);

    if (self.button.frame.origin.y + self.button.frame.size.height < heightScreen)
    {
        self.contentSize = CGSizeMake(widthScreen,heightScreen + 20);
    }
    else
    {
        self.contentSize = CGSizeMake(widthScreen, self.button.frame.origin.y + self.button.frame.size.height + 50);
    }
    self.showsVerticalScrollIndicator = NO;
    
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat y = scrollView.contentOffset.y;
//    if (y <= 0) {
//        CGRect frame = self.upImageView.frame;
//        frame.origin.y = y;
//        frame.size.height =  -y;
//        self.upImageView.frame = frame;
//    }
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
